﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ProjectHopur23.Models;
using ProjectHopur23.Repositories;

namespace ProjectHopur23.Controllers
{
	public class LudoController : Controller
	{
		#region Variables
		private IRepository rep = null;
		#endregion

		#region Constructors
		public LudoController(IRepository r)
		{
			rep = r;
		}

		public LudoController()
		{
			rep = new Repository();
		}
		#endregion

		#region Functions
		[Authorize]
		public ActionResult Playing(int? id)
		{
			if(id == null)
			{
				return View("Error");
			}

			GameController gc = new GameController();
			ActiveGame ag = gc.GetActiveGameByID(id.Value);
			if(gc.GameIsValidInstance(ag, "Ludo", HttpContext.User.Identity.Name) == false)
			{
				return View("Error");
			}

			GameViewModel ludo = new GameViewModel();
			ludo.listOfPlayers = gc.GetAllUsersInActiveGame(id.Value);
			ludo.theActiveGame = ag;
			ludo.comments = from c in rep.GetAllComments()
					where c.activeGameId == ag.ID
					select c;

			LudoState[] gameStates = (from l in rep.GetAllLudoStates()
							where l.activeGameId == ag.ID
							select l).ToArray();

			if(gameStates.Any() == false)
			{
				gameStates = new LudoState[4*ag.numberOfPlayers];

				if(ag.numberOfPlayers >= 2)
				{
					// Initial pieceCounter setting
					int counter = 56;

					gameStates[0] = new LudoState {activeGameId = ag.ID, pieceId = 0, cellId = 100, playerId = 0, pieceCounter = counter};
					gameStates[1] = new LudoState {activeGameId = ag.ID, pieceId = 1, cellId = 101, playerId = 0, pieceCounter = counter};
					gameStates[2] = new LudoState {activeGameId = ag.ID, pieceId = 2, cellId = 102, playerId = 0, pieceCounter = counter};
					gameStates[3] = new LudoState {activeGameId = ag.ID, pieceId = 3, cellId = 103, playerId = 0, pieceCounter = counter};
					gameStates[4] = new LudoState {activeGameId = ag.ID, pieceId = 4, cellId = 104, playerId = 1, pieceCounter = counter};
					gameStates[5] = new LudoState {activeGameId = ag.ID, pieceId = 5, cellId = 105, playerId = 1, pieceCounter = counter};
					gameStates[6] = new LudoState {activeGameId = ag.ID, pieceId = 6, cellId = 106, playerId = 1, pieceCounter = counter};
					gameStates[7] = new LudoState {activeGameId = ag.ID, pieceId = 7, cellId = 107, playerId = 1, pieceCounter = counter};

					if(ag.numberOfPlayers >= 3)
					{
						gameStates[8] = new LudoState {activeGameId = ag.ID, pieceId = 8, cellId = 108, playerId = 2, pieceCounter = counter};
						gameStates[9] = new LudoState {activeGameId = ag.ID, pieceId = 9, cellId = 109, playerId = 2, pieceCounter = counter};
						gameStates[10] = new LudoState {activeGameId = ag.ID, pieceId = 10, cellId = 110, playerId = 2, pieceCounter = counter};
						gameStates[11] = new LudoState {activeGameId = ag.ID, pieceId = 11, cellId = 111, playerId = 2, pieceCounter = counter};

						if(ag.numberOfPlayers >= 4)
						{
							gameStates[12] = new LudoState {activeGameId = ag.ID, pieceId = 12, cellId = 112, playerId = 3, pieceCounter = counter};
							gameStates[13] = new LudoState {activeGameId = ag.ID, pieceId = 13, cellId = 113, playerId = 3, pieceCounter = counter};
							gameStates[14] = new LudoState {activeGameId = ag.ID, pieceId = 14, cellId = 114, playerId = 3, pieceCounter = counter};
							gameStates[15] = new LudoState {activeGameId = ag.ID, pieceId = 15, cellId = 115, playerId = 3, pieceCounter = counter};
						}
					}
				}

				for(int i = 0; i < 4 * ag.numberOfPlayers; i++)
				{
					rep.AddLudoState(gameStates[i]);
				}

				DieValue theDie = new DieValue {activeGameId = ag.ID, dieValue = 0, dieRolled = false};
				rep.AddDieValue(theDie);

				rep.Save();
			}

			return View(ludo);
		}
		#endregion
	}
}
